#!/bin/bash

#test # arguments.
if [[ $# -gt 3  || $# == 0 ]]; then 
  echo "Usage: $0 characteristic_type <color_characteristic_type>"
  echo "  or" 
  echo "       $0 characteristic_type color_characteristic_type <fullctdinfo>"
  exit
fi

cttype=$1
if [[ $# -ge 2 ]]; then 
  colorcttype=$2
else
  colorcttype=1
fi

if [[ $# == 3 ]]; then
  ctdinfotype=1
  ctdinfostring=ctdfullinfo
else
  ctdinfotype=2
  ctdinfostring=ctdinfo
fi

imagedir=testimages/
resultdir=testresults/
optimizerdir=../optimizer/
echo "testing characteristic type $cttype, color characteristic type $colorcttype"
echo " images in $imagedir, results placed in $resultdir"

if [[ ! -e $resultdir ]]; then 
   mkdir $resultdir
fi

optionstring=ct$cttype-cct$colorcttype

#show commands as they run.
#set -x 

for fullimgname in $imagedir/*.p[pg]m
do
#remove directory name from fullimgname
  imgname=`basename $fullimgname`     
  currresultdir=$resultdir/$imgname 
  if [[ ! -e $currresultdir ]]; then 
     mkdir $currresultdir
  fi
  fulllogname=$currresultdir/logfile.txt
  echo "processing $imgname"

  greedyname=${optionstring}greedy
#create greedy ctd file, if necessary. appends to existing log.
  if [[ ! -e $currresultdir/$greedyname.ctd ]]; then
     if [[ ! -e $currresultdir/$optionstring.ccd ]]; then
        echo "******Creating ccd file " `date` >> $fulllogname
        imggenccd -ct $cttype -cct $colorcttype -out 1 $imagedir/$imgname $currresultdir/$optionstring >> $fulllogname
     fi

     echo "******Creating ctd " `date` >> $fulllogname
     echo "******Creating ctd " `date` >> $currresultdir/${optionstring}$ctdinfostring.txt
     imggenccd -ct $cttype -cct $colorcttype -out 2 $imagedir/$imgname $currresultdir/$optionstring | txt2bytes - | java -Xmx1024m -cp $optimizerdir Main -d $ctdinfotype --cc -  --ccd $currresultdir/$optionstring.ccd --ctd $currresultdir/$greedyname.ctd >> $currresultdir/${optionstring}$ctdinfostring.txt
  else 
#ctd already exists; if ctdfullinfo requested and doesn't exist, generate it
     if [[ $ctdinfostring == ctdfullinfo ]]; then
         if [[ ! -e $currresultdir/$optionstring$ctdinfostring.txt ]]; then
	     echo "******Creating ctdfullinfo " `date` >> $fulllogname
	     echo "******Creating ctdfullinfo " `date` >> $currresultdir/${optionstring}$ctdinfostring.txt
	     imggenccd -ct $cttype -cct $colorcttype -out 2 $imagedir/$imgname $currresultdir/$optionstring | txt2bytes - | java -Xmx1024m -cp $optimizerdir Main -d $ctdinfotype --cc -  --ccd $currresultdir/$optionstring.ccd --ctd $currresultdir/temp.ctd >> $currresultdir/${optionstring}$ctdinfostring.txt
	     rm $currresultdir/temp.ctd
	 fi
     fi
  fi

  if [[ ! -e $currresultdir/$greedyname.bits ]]; then
     echo "******Creating bits file " `date` >> $fulllogname
     contextencode -ct $cttype -cct $colorcttype  -cf $currresultdir/$greedyname.ctd $imagedir/$imgname $currresultdir/$greedyname.bits >> $fulllogname
  fi 

  if [[ ! -e $currresultdir/${greedyname}bsstats.txt ]]; then 
     echo "******Creating bsstats file for $greedyname" `date` >> $fulllogname
     contextdecode -bp 15 -bs 1 -i $imagedir/$imgname $currresultdir/$greedyname.bits $currresultdir/temp$imgname > $currresultdir/${greedyname}bsstats.txt
  fi

  if [[ ! -e $currresultdir/${greedyname}stats.txt ]]; then 
#get size of image (in 512-byte blocks) 
     numblocks=`ls -Ls $imagedir/$imgname | cut -d \  -f1`
     echo "******Creating stats file for $greedyname" `date` >> $fulllogname
# numbytes per iteration depends on filesize; about 10000 for 512 x 512 grayscale images.
     contextdecode -bp 15 -bs `expr $numblocks \* 20` -i $imagedir/$imgname $currresultdir/$greedyname.bits $currresultdir/temp$imgname > $currresultdir/${greedyname}stats.txt
  fi


#delete useless decompressed image. 
  if [[ -e $currresultdir/temp$imgname ]]; then 
     rm $currresultdir/temp$imgname
  fi
#cleanup, removing huge .cc file
  if [[ -e $currresultdir/$optionstring.cc ]]; then
     rm $currresultdir/$optionstring.cc
  fi
#  possible future sorting stats. 
#  contextencode -ct 1 -cf goodcc/lenna1greedy.ctd -e 0 -s 8  $imgname temp/temp.bits
#  contextdecode -cf goodcc/lenna1greedy.ctd -i $imgname temp/temp.bits temp/temp.pgm

done





